package com.guli.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }
    
    //三级树优化
    @Override
    public List<CategoryEntity> treelist(){
        //1.查询所有的三级分类信息(数据库只交互一次)
        List<CategoryEntity> allList = baseMapper.selectList(null);

        //2.在allList中获取一级分类集合
        //item是集合中的每个元素
       List<CategoryEntity> l1List= allList.stream().filter(item->{

            return item.getParentCid().equals(0l);
        }).map(item->{ //item是过滤后的结果
            //需要在这个位置找二级分类
           //item.setChildren();
           //找子元素的方法
           List<CategoryEntity> children = getChildren(allList, item);//得到二级分类（应该包含三级分类）
           item.setChildren(children);
           return item;
        }).sorted((s1,s2)->{

            return (s1.getSort()!=null?s1.getSort():0)-(s2.getSort()!=null?s2.getSort():0);
       }).collect(Collectors.toList());
        return  l1List;
    }

    private List<CategoryEntity> getChildren(List<CategoryEntity> allList,CategoryEntity categoryEntity){
       List<CategoryEntity> children=allList.stream().filter(item->{
            return item.getParentCid().equals(categoryEntity.getCatId());

        }).map(item->{
            //查询三级分类
            List<CategoryEntity> children1 = getChildren(allList, item);
            item.setChildren(children1);
            return item;
        }).sorted((s1,s2)->{

           return (s1.getSort()!=null?s1.getSort():0)-(s2.getSort()!=null?s2.getSort():0);
       }).collect(Collectors.toList());

        return children;
    };

//    @Override
//    public List<CategoryEntity> treelist() {
//
//        //1.三级分类树
//        //(1）查询一级分类
//        //(2）遍历所有的一级分类查询二级
//        //（3）遍历所有的二级分类查询三级
//        QueryWrapper<CategoryEntity> w1 = new QueryWrapper<>();
//        //a.查询一级分类
//        w1.eq("parent_cid",0);
//        List<CategoryEntity> l1list = baseMapper.selectList(w1);
//
//        for (CategoryEntity l1 :l1list){
//            Long catId = l1.getCatId();
//            QueryWrapper<CategoryEntity> w2 = new QueryWrapper<>();
//            w2.eq("parent_cid",catId);
//            List<CategoryEntity> l2list = baseMapper.selectList(w2);
//            l1.setChildren(l2list);
//            for (CategoryEntity l2 :l2list){
//
//                List<CategoryEntity> l3list =baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid",l2.getCatId()));
//                l2.setChildren(l3list);
//
//            }
//        }
//        return l1list;
//    }

}